import os
from typing import Optional


class Settings:
    """
    应用配置类
    """

    # 项目基本信息
    PROJECT_NAME: str = "Stable Matching Visualization"
    PROJECT_VERSION: str = "1.0.0"
    PROJECT_DESCRIPTION: str = "Visualization tool for container-route stable matching"

    # API配置
    API_V1_STR: str = "/api"
    DEBUG: bool = True

    # C++可执行文件路径
    CPP_EXECUTABLE_PATH: str = os.path.join(
        os.path.dirname(__file__),
        "cmake-build-debug",
        "stable_match.exe" if os.name == 'nt' else "stable_match"
    )

    # 数据文件路径
    DATA_DIR: str = os.path.join(
        os.path.dirname(__file__),
        "cmake-build-debug",
        "data"
    )

    NETWORK_DATA_FILE: str = os.path.join(DATA_DIR, "network.csv")
    SHIPMENT_DATA_FILE: str = os.path.join(DATA_DIR, "shipment.csv")
    ROUTE_DATA_FILE: str = os.path.join(DATA_DIR, "route.csv")
    COOPERATION_DATA_FILE: str = os.path.join(DATA_DIR, "cooperation_parameter.csv")

    # 结果文件路径
    RESULT_DIR: str = os.path.join(
        os.path.dirname(__file__),
        "cmake-build-debug",
        "result"
    )

    # 服务器配置
    HOST: str = "127.0.0.1"
    PORT: int = 8000

    # WebSocket配置
    WEBSOCKET_HOST: str = "127.0.0.1"
    WEBSOCKET_PORT: int = 8001

    # 缓存配置
    MAX_CACHE_SIZE: int = 100  # 最大缓存项目数
    CACHE_TTL: int = 3600  # 缓存过期时间（秒）

    @property
    def data_files_exist(self) -> bool:
        """
        检查数据文件是否存在
        """
        return all([
            os.path.exists(self.NETWORK_DATA_FILE),
            os.path.exists(self.SHIPMENT_DATA_FILE),
            os.path.exists(self.ROUTE_DATA_FILE),
            os.path.exists(self.COOPERATION_DATA_FILE)
        ])

    @property
    def cpp_executable_exists(self) -> bool:
        """
        检查C++可执行文件是否存在
        """
        return os.path.exists(self.CPP_EXECUTABLE_PATH)


# 创建全局配置实例
settings = Settings()
